package com.javaclimb.service.impl;

import com.javaclimb.pojo.SysUser;
import com.javaclimb.mapper.SysUserMapper;
import com.javaclimb.service.SysUserService;
import com.javaclimb.util.Consts;
import com.javaclimb.util.ReturnData;
import com.javaclimb.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员管理的service实现接口
 */
@Service
public class SysUserServiceImp implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 用户登录判断，返回登录是否成功的对象
     */
    @Override
    public ReturnData login(SysUser user, HttpServletRequest request) {
        //默认显示登陆失败
        ReturnData returnData = ReturnData.fail("登录失败");
        if(user==null){
            //如果传递的user就显示非法调用
            returnData = ReturnData.fail("非法调用");
        }else if(TextUtils.isEmpty(user.getUsername())){
            //判断user的账户为空或者未0的话
            returnData = ReturnData.fail("用户名不能为空");
        }else if(TextUtils.isEmpty(user.getPassword())){
            //判断密码未空或者未0的话
            returnData = ReturnData.fail("密码不能为空");
        }else{
            //查询有没有这个用户(通过用户名进行查询)
            SysUser dbUser = sysUserMapper.getUserByUserName(user.getUsername());
            if(dbUser==null){
                //为空就返回用户不存在
                returnData = ReturnData.fail("用户不存在");
            }else if(!dbUser.getPassword().equals(user.getPassword())){
                //密码不正确的话返回密码错误
                returnData = ReturnData.fail("密码错误");
            }else{
                //全部对的话，就把用户名存放到session里面并且显示登陆成功
                request.getSession().setAttribute(Consts.SYS_USER_INFO,dbUser);
                returnData = ReturnData.success("登录成功");
            }
        }
        return returnData;
    }
}
